import 'dart:collection';
import 'dart:core';
import 'dart:core';

import 'package:elevator/model/alerm_info.dart';
import 'package:elevator/model/base_model.dart';
import 'package:elevator/model/build_detail.dart';
import 'package:elevator/model/group_info.dart';
import 'package:elevator/model/pum_info.dart';
import 'package:elevator/model/pus_info.dart';
import 'package:elevator/model/tcp_info.dart';
import 'package:elevator/model/warn_info.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:mysql1/src/single_connection.dart';

import 'lift_info.dart';

class BuildInfo extends BaseModel{
  String buildId = "";

  BuildDetail buildDetail = BuildDetail();

  /// 要显示的状态信息
  Map<int,LiftInfo> liftInfoStateMaps = <int,LiftInfo>{};

  ///对应数据库 tbAppCfgInfo表 电梯楼层显示信息配置  fn1~fn99
  List<String> fnInfoList = [];

  ///数据库 tbapplwarncfg 普通梯配置的所有警告信息
  Map<int,WarnInfo> warnInfoMap =  <int,WarnInfo>{};

  ///数据库 tbappelwarncfg 扶手梯梯配置的3-8警告信息
  Map<int,WarnInfo> eWarnInfoMap =  <int,WarnInfo>{};

  ///数据库 tbtpccfg表 TPC配置警告信息
  Map<int,List<TpcInfo>> tpcInfoMap =  <int,List<TpcInfo>>{};

  ///数据库 tbpumcfg表 TPC配置警告信息
  Map<int,PumInfo> pumInfoMap =  <int,PumInfo>{};

  ///数据库 tbpuscfg表 TPC配置警告信息
  Map<int,PusInfo> pusInfoMap =  <int,PusInfo>{};

  ///对应数据库 tbAppCfgInfo 表 IsPush1~32
  List<int> pushInfoList = [];

  List<GroupInfo> groupList = [];

  GroupInfo alarmGrop = GroupInfo(id:0,name: "Alarm");


  BuildInfo(){
    groupList.add(alarmGrop);
  }

  @override
  String toString() {
    return 'BuildInfo{buildId: $buildId, liftInfoStateMaps: $liftInfoStateMaps}';
  }

  void initLiftInfoStateMaps(LiftInfo liftInfo){
    LiftInfo liftStateInfo = LiftInfo();
    liftStateInfo.order = liftInfo.order;
    liftStateInfo.liftName = liftInfo.liftName;
    liftStateInfo.liftType = liftInfo.liftType;
    liftStateInfo.liftOpenType = liftInfo.liftOpenType;
    if(null != liftInfo.liftName && liftInfo.liftName.isNotEmpty){
      liftInfoStateMaps[liftStateInfo.order] = liftInfo;
    }
  }

  ///转换电梯楼层信息
  String getLiftAddr(int currentAddr) {
    if(fnInfoList.length>currentAddr-1){
      return fnInfoList[currentAddr-1];
    }
    return "";
  }

  ///转换普通垂直电梯楼层状态信息 0 up, 1 down,2 move,3 bell ,-1 未知
  int getLiftState(int currentState) {
     String data = intToRadixString(currentState);
     if(0=="0".compareTo(data.substring(0,1))){
       return 0;
     }else if(0=="0".compareTo(data.substring(1,2))){
       return 1;
     }else if(0=="0".compareTo(data.substring(2,3))){
       return 2;
     }else if(0=="0".compareTo(data.substring(3,4))){
       return 3;
     }
     return -1;
   }

   static String intToRadixString(int value){
     String data = value.toRadixString(2);
     int length = 8 - data.length;
     if(length>0){
       for(int i = length;i>0;i-- ){
         data = "0$data";
       }
     }
     return data;
   }

  String intToRadix16String(int value){
    String data = value.toRadixString(16).toUpperCase();
    int length = 2 - data.length;
    if(length>0){
      for(int i = length;i>0;i-- ){
        data = "0$data";
      }
    }
    return data;
  }

  ///将字符串反过来
  static String reversedStr(String data){
    return data.split('').reversed.join();
  }

  List<AlermInfo> getSpvAlermInfo(LiftInfo liftInfo,int alerm1, int alerm2, int alerm3, int alerm4) {
    StringBuffer alermBuffer = StringBuffer(reversedStr(intToRadixString(alerm1)));
    alermBuffer.write(reversedStr(intToRadixString(alerm2)));
    alermBuffer.write(reversedStr(intToRadixString(alerm3)));
    alermBuffer.write(reversedStr(intToRadixString(alerm4)));
    String alermStr = alermBuffer.toString();
    liftInfo.svpStr = alermStr;
    List<AlermInfo> alermList = [];
    if(!alermStr.contains("0")){
      return alermList;
    }
    bool liftOpen = false;
    for(int i=0;i<alermStr.length;i++){
      String char = alermStr.substring(i,i+1);
      if(char.compareTo("0")==0){
        if(0==liftInfo.liftType){ ///0为普通梯，1为扶手梯
          if(i==12){ ///普通梯前门开
            liftOpen = true;
            liftInfo.liftOpenType = 3;
          }else if(i==15){ ///普通梯侧门开
            liftOpen = true;
            liftInfo.liftOpenType = 2;
          }else if(warnInfoMap.containsKey(i+1)){  //数据库的从1开始计数
            if(null!=warnInfoMap[i+1] && ""!=warnInfoMap[i+1]?.sName){
              AlermInfo alermInfo = AlermInfo.buildFormWarnInfo(warnInfoMap[i+1]!);
              alermList.add(alermInfo);
            }
          }
        }else{
          liftInfo.liftOpenType = 0;
          if(i==16){ ///扶手梯 上
            liftInfo.currentState = 0;
          }else if(i==17){ ///扶手梯 下
            liftInfo.currentState = 1;
          }else if(18<=i && i<24){ ///扶手梯警示信息
              int number = i-15;
              if(eWarnInfoMap.containsKey(number)){
                AlermInfo alermInfo = AlermInfo.buildFormWarnInfo(eWarnInfoMap[number]!);
                alermList.add(alermInfo);
              }
          }
        }
      }
    }
    if(0==liftInfo.liftType && !liftOpen){
      liftInfo.liftOpenType = 1;
    }
    return alermList;
  }

  void loadAlermInfo(LiftInfo liftInfo){
    if(null != liftInfo){
      liftInfo.alermList.clear();
      if(null != liftInfo.tpcAlermList || null != liftInfo.spvAlermList){
        liftInfo.alermList.addAll(liftInfo.tpcAlermList);
        liftInfo.alermList.addAll(liftInfo.spvAlermList);
        liftInfo.alermList.sort((a, b) => a.compareTo(b));
      }
      if(liftInfo.alermList.isNotEmpty && !alarmGrop.liftList.contains(liftInfo)){
        alarmGrop.liftList.add(liftInfo);
      }else if( liftInfo.alermList.isEmpty && alarmGrop.liftList.contains(liftInfo)){
        alarmGrop.liftList.remove(liftInfo);
      }
    }
  }

  @override
  String getTag() {
    return buildDetail.getTag();
  }

  ///加载该楼盘的所有电梯信息
  void loadLiftInfo(Results liftList) {
    if(null != liftList){
      for (var row in liftList) {
        if(null != row && row.length>=4){
          LiftInfo liftInfo;
          int id = row[0];
          int liftAddr = row[1];
          String liftName = row[2].toString();
          int liftType = row[3];
          if(liftInfoStateMaps.containsKey(liftAddr)){
            liftInfo = liftInfoStateMaps[liftAddr]!;
          }else{
            liftInfo = LiftInfo();
          }
          liftInfo.order = liftAddr;
          liftInfo.liftName = liftName;
          liftInfo.liftType = liftType;
          if(liftType==0){
            liftInfo.liftOpenType = 1;
          }
          liftInfoStateMaps[liftAddr] = liftInfo;
        }
      }
      debugPrint("楼盘 $buildId 电梯信息加载完毕，共：${liftInfoStateMaps.length}条");
    }
  }
  ///加载该楼盘的所有电梯楼层显示信息
  void loadFnInfo(Results fnInfoList) {
    for (var row in fnInfoList) {
      if(null != row){
        for(int i=1;i<100;i++){
          String value = "";
          if(null!=row[i]){
            value = row[i].toString();
          }else{
            value = "";
          }
          this.fnInfoList.add(value);
        }
      }
    }
  }

  ///加载电梯警告配置
  void loadWarnInfo(Results warnInfoList) {
    if(null != warnInfoList){
      for (var row in warnInfoList) {
        if(null != row && row.length>=7){
          WarnInfo warnInfo = WarnInfo.builder(row);
          warnInfoMap[warnInfo.number] = warnInfo;
        }
      }
      debugPrint("楼盘 $buildId 警报配置加载完毕，共：${warnInfoMap.length}条");
    }
  }

  ///加载手扶梯电梯警告配置
  void loadEWarnInfo(Results eWarnInfoList) {
    if(null != eWarnInfoList){
      for (var row in eWarnInfoList) {
        if(null != row && row.length>=7){
          WarnInfo warnInfo = WarnInfo.builder(row);
          eWarnInfoMap[warnInfo.number] = warnInfo;
        }
      }
      debugPrint("楼盘 $buildId 手扶梯警报配置加载完毕，共：${eWarnInfoMap.length}条");
    }
  }

  void loadGroupInfo(int id, Results group1list) {
    GroupInfo groupInfo = GroupInfo.build(id,group1list,liftInfoStateMaps);
    groupList.add(groupInfo);
  }

  void loadTpcCfg(Results tpccfgList) {
    if(null != tpccfgList){
      for (var row in tpccfgList) {
        if(null != row && row.length>=7){
          TpcInfo tpcInfo = TpcInfo.builder(row);
          if(tpcInfoMap.containsKey(tpcInfo.tpcAddr)){
            tpcInfoMap[tpcInfo.tpcAddr]?.add(tpcInfo);
          }else{
            List<TpcInfo> tcpInfoList = [];
            tcpInfoList.add(tpcInfo);
            tpcInfoMap[tpcInfo.tpcAddr] = tcpInfoList;
          }
        }
      }
      debugPrint("楼盘 $buildId tpc警报配置加载完毕，共：${tpcInfoMap.length}条");
    }
  }

  void loadPumCfg(Results pumcfgList) {
    for (var row in pumcfgList) {
      if(row.length>=6){
        PumInfo pumInfo = PumInfo.builder(row);
        pumInfoMap[pumInfo.pumValue] = pumInfo;
      }
    }
    debugPrint("楼盘 $buildId pum配置加载完毕，共：${pumInfoMap.length}条");
    }

  void loadPusCfg(Results puscfgList) {
    if(null != puscfgList){
      for (var row in puscfgList) {
        if(null != row && row.length>=6){
          PusInfo pusInfo = PusInfo.builder(row);
          pusInfoMap[pusInfo.pusValue] = pusInfo;
        }
      }
      debugPrint("楼盘 $buildId pum配置加载完毕，共：${pusInfoMap.length}条");
    }
  }

  void loadBuildDetail(buildDetailList) {
    if(null != buildDetailList){
      for (var row in buildDetailList) {
        if(null != row && row.length>=17){
          BuildDetail buildDetail = BuildDetail.builder(row);
          this.buildDetail = buildDetail;
          debugPrint("楼盘 $buildId 详细信息加载完毕");
          return;
        }
      }
    }
  }
}